#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.Text;

using Osh.Media;
using Osh.Teletext;

namespace Osh.Subtitles
{
	public class SubtitleProvider : ISubtitleProvider
	{
		private IMediaPlayer _player;
		private IMedia _media;
		private ISubtitleStream _subtitleStream;
		private ISubtitleSource _subtitleSource;

		public SubtitleProvider()
		{
			OshHostBase.Current.Osh.MediaController.ActiveMediaPlayerChange +=
				new EventHandler<Osh.Media.MediaPlayerEventArgs>(MediaController_ActiveMediaPlayerChange);
		}

		private void MediaController_ActiveMediaPlayerChange(object sender, Osh.Media.MediaPlayerEventArgs e)
		{
			if(e.MediaPlayer == _player)
				return;

			if(_player != null)
			{
				_player.MediaChange -= new EventHandler<MediaChangeArgs>(MediaPlayer_MediaChange);
				_player.MediaStreamChange -= new EventHandler<MediaStreamChangeArgs>(MediaPlayer_MediaStreamChange);
				if(_player.Timeline != null)
					_player.Timeline.Change -= new EventHandler<TimelineChangeArgs>(MediaPlayer_Timeline_Change);
				if(_subtitleSource != null)
					_subtitleSource.SubtitlesReady -= new EventHandler<SubtitlesEventArgs>(SubtitleSource_SubtitlesReady); 
				_player = null;
				_media = null;
				_subtitleStream = null;
				_subtitleSource = null;
			}

			if(e.MediaPlayer == null)
				return;

			_player = e.MediaPlayer;
			_player.MediaChange += new EventHandler<MediaChangeArgs>(MediaPlayer_MediaChange);
			_player.MediaStreamChange += new EventHandler<MediaStreamChangeArgs>(MediaPlayer_MediaStreamChange);
			if(_player.Timeline != null)
				_player.Timeline.Change += new EventHandler<TimelineChangeArgs>(MediaPlayer_Timeline_Change);
		}

		private void MediaPlayer_MediaChange(object sender, MediaChangeArgs e)
		{
			if(_media == e.Media)
				return;

			_media = e.Media;
			ConnectToSubtitleStream();
		}

		private void MediaPlayer_MediaStreamChange(object sender, MediaStreamChangeArgs e)
		{
			if(e.MediaStreamDescriptor is ISubtitleStream)
				ConnectToSubtitleStream();
		}

		private void MediaPlayer_Timeline_Change(object sender, TimelineChangeArgs e)
		{
			//e.TimelineInfo.CurrentTimeUtc
		}

		private void ConnectToSubtitleStream()
		{
			if(_player == null || _media == null)
				return;

			ISubtitleStream stream = null;
			if(_media is IDvbChannel)
				stream = (_media as IDvbChannel).CurrentSubtitles;
			else if(_media is IVideoFile)
				stream = (_media as IVideoFile).CurrentSubtitles;

			if(_subtitleStream == stream)
				return;

			if(_subtitleStream != null)
				_subtitleSource.SubtitlesReady -= new EventHandler<SubtitlesEventArgs>(SubtitleSource_SubtitlesReady); 

			_subtitleStream = stream;

			if(_subtitleStream is ITeletextSubtitleStream)
			{
				TeletextService service = OshHostBase.Current.Osh.MediaController.TeletextParser.TeletextService;
				if(service != null)
					_subtitleSource = new TeletextSubtitleParser(service,
						(_subtitleStream as ITeletextSubtitleStream).PageAddress);
			}

			if(_subtitleSource != null)
				_subtitleSource.SubtitlesReady += new EventHandler<SubtitlesEventArgs>(SubtitleSource_SubtitlesReady); 
		}

		private void SubtitleSource_SubtitlesReady(object sender, SubtitlesEventArgs e)
		{
			//e.SubtitlesHtml
		}

		protected virtual void OnSubtitlesChange(SubtitlesEventArgs args)
		{
			if(this.SubtitlesChange != null)
				this.SubtitlesChange(null, args);
		}

		public int Delay
		{
			get { throw new NotImplementedException(); }
			set { throw new NotImplementedException(); }
		}
		public event EventHandler<SubtitlesEventArgs> SubtitlesChange;

	}
}
